// pages/my/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isAttendance: true,
        AttendanceCount: 0,
        studentStatus: null,
        finalStudentStatus:null,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.checkAttendance();
        this.getStudentStatus();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    //获取学习状态
    getStudentStatus: function () {
        var that=this;
        wx.request({
            url: app.buildUrl('/student/status'),
            header: app.getRequestHeader(),
            method: 'POST',
            success: res => {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                that.setData({
                    studentStatus:resp.data[0]
                })
                this.setStudentStatus();
            }
        })
    },
    // 设置学生状态
    setStudentStatus:function(){
        var that=this;
        var nowStatus=that.data.studentStatus;
        if(nowStatus.status==1){
            that.setData({
                finalStudentStatus:nowStatus.statusName
            })
        }else if(nowStatus.status==2){
            that.setData({
                finalStudentStatus:`当前状态：${nowStatus.statusName} | 目标职位：${nowStatus.professionName}`
            })
        }else if(nowStatus.status==3){
            that.setData({
                finalStudentStatus:`当前状态：${nowStatus.statusName} | 目标职位：${nowStatus.professionName}`
            })
        }
    },
    //检验签到是否成功
    checkAttendance: function () {
        var that = this;
        wx.request({
            url: app.buildUrl('/student/check-attendance'),
            header: app.getRequestHeader(),
            method: 'POST',
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                if (resp.data.dictValue == 1) {
                    that.setData({
                        isAttendance: true,
                        AttendanceCount: resp.data.count
                    })
                } else {
                    that.setData({
                        isAttendance: false,
                    })
                }
            }
        })
    },
    Attendance: function () {
        var that = this;
        wx.request({
            url: app.buildUrl('/student/attendance'),
            header: app.getRequestHeader(),
            method: 'POST',
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg});
                    return;
                }
                wx.showToast({
                    title: '签到成功',
                    icon: 'success',
                    duration: 1000,
                    success: function () {
                        that.setData({
                            isAttendance: true,
                            AttendanceCount: resp.data.count
                        })
                    }
                })
            }
        })

    },
    tel:function () {
        wx.makePhoneCall({
            phoneNumber: app.globalData.phoneNumber,
        })
    }
})